//
//  MCChooseMultiplePicturesView.swift
//  CityPlatform
//
//  Created by MC on 2018/12/19.
//  Copyright © 2018 MC. All rights reserved.
//

import UIKit
import GMJKFunction
import GMJKPublicUI


class MCChooseMultiplePicturesView: UIView {
    
    public var isNeedEditImage = false
    public var superViewController : UIViewController?
    
    public var bigImageCount = 6
    
    // [UIImage]()
    public var dataArrayM : NSMutableArray = NSMutableArray() {
        didSet {
            self.colleciontView.reloadData()
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.white
        
        self.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(self)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(30)
        }
        
        self.addSubview(colleciontView)
        colleciontView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.top.equalTo(promptLabel.snp.bottom).offset(0)
            make.bottom.equalTo(self).offset(0)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.jk14
        label.text = "选择图片"
        label.textColor = UIColor.jk_darkGray
        return label
    }()
    

    
    
    @objc func deleteEvent(sender:UIButton) {
        let tag = sender.tag - 1000
        dataArrayM.removeObject(at: tag)
        colleciontView.reloadData()
    }
    
    func chooseImage() {
        
        if superViewController == nil {
            return
        }
        
        
        if let vc = superViewController {
            UIAlertController.jk_chooseImage(on: vc) { (title) in
                
                if title == "拍照" {
                    JKPickerImageHelper.jk_openCamera(vc, completeHandler: { (image) in
                        self.dataArrayM.add(image)
                        self.colleciontView.reloadData()
                    })
                } else {
                    JKPhotoLibraryHelper.open(target: vc, maxSelectCount: 3) { (images) in
                        
                        for image in images {
                            self.dataArrayM.add(image)
                            self.colleciontView.reloadData()
                        }
                    }
                }
            }
        }
    }
    
    
    
    
    
    
    private lazy var colleciontView: JKCollectionView = {
        
        //  68  97
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
        
        let margin = 10
        let itemWidth : CGFloat = 90
        let itemHeight : CGFloat = 90
        
        layout.sectionInset = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
        
        layout.itemSize = CGSize.init(width: itemWidth, height: itemHeight)
        layout.minimumLineSpacing = CGFloat(margin)
        layout.minimumInteritemSpacing = CGFloat(margin)
        
        let collectionView = JKCollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.showsHorizontalScrollIndicator = false
        
        collectionView.jk_registerCell(UICollectionViewCell.self)
        collectionView.jk_registerCell(MCChooseMultiplePicturesCell.self)

        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        return collectionView
    }()
}

extension MCChooseMultiplePicturesView : UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if dataArrayM.count >= bigImageCount {
            return dataArrayM.count
        } else {
            return dataArrayM.count + 1
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if dataArrayM.count >= bigImageCount {
            let cell = MCChooseMultiplePicturesCell.customCell(collectionView: collectionView, indexPath: indexPath)
            
            for view in cell.contentView.subviews {
                view.removeFromSuperview()
            }
            cell.showImageView.image = self.dataArrayM[indexPath.row] as? UIImage
            cell.row = indexPath.row
            cell.delegate = self
            return cell
        }
        
        
        if indexPath.row == dataArrayM.count {
            
            
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: JKTool.jk_getClassName(UICollectionViewCell.self), for: indexPath)
            
            for view in cell.contentView.subviews {
                view.removeFromSuperview()
            }
            
            let imageView = UIImageView()
            imageView.image = UIImage.init(named: "Store_addImages")
            imageView.layer.borderColor = UIColor.jk_line.cgColor
            imageView.layer.borderWidth = 1
            cell.contentView.addSubview(imageView)
            imageView.snp.remakeConstraints { (make) ->Void in
                make.edges.equalTo(cell.contentView)
            }
            return cell
        } else {
            let cell = MCChooseMultiplePicturesCell.customCell(collectionView: collectionView, indexPath: indexPath)

            for view in cell.contentView.subviews {
                view.removeFromSuperview()
            }
            cell.showImageView.image = self.dataArrayM[indexPath.row] as? UIImage
            cell.row = indexPath.row
            cell.delegate = self
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if dataArrayM.count == 0 {
            self.chooseImage()
        } else {
            
            if dataArrayM.count >= bigImageCount {
                print("点击了图片")
            } else {
                if indexPath.row == dataArrayM.count {
                    self.chooseImage()
                } else {
                    print("点击了图片")
                }
            }
            
        }
    }
}

extension MCChooseMultiplePicturesView : MCChooseMultiplePicturesCellProtocol {
    func deleteImage(row: Int) {
        self.dataArrayM.removeObject(at: row)
        self.colleciontView.reloadData()
    }
}
